package com.jgp.security.shiro.controller;

import com.jgp.security.props.SecurityConfigPropBean;
import com.jgp.security.shiro.RSAUtils;
import com.jgp.sys.controller.JGPBackController;
import com.jgp.sys.ui.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2020/11/10
 */
@RestController
@RequestMapping("/security/entry-api-ctl")
public class EntryApiController extends JGPBackController {
    
    @Autowired
    private SecurityConfigPropBean securityConfigPropBean;
    
    @PostMapping(value = "/get-pbk")
    public Result getPbk() {
        if(securityConfigPropBean.getRsa()){
            Subject subject = SecurityUtils.getSubject();
            Map<String,String> pairs = null;
            try {
                pairs = RSAUtils.createKeyPairs();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            String privateKey = pairs.get("privateKey");
            String publicKey = pairs.get("publicKey");
            subject.getSession().setAttribute("privateKey",privateKey);
            return ajaxRe(true).addData("publicKey", publicKey);
        }else{
            return ajaxRe(false);
        }
        
    }
    
    @RequestMapping(value = "/check-login")
    public Result checkLogin() {
        return ajaxRe(true);
    }
}
